{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Relax RemoveRedundantReshape: 消除冗余的 reshape\n",
    "\n",
    "参考：`python/tvm/relax/transform/remove_redundant_reshape.py`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/media/pc/data/lxw/ai/tvm-book/doc/read\n"
     ]
    }
   ],
   "source": [
    "%cd ..\n",
    "import set_env"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm.testing\n",
    "from tvm import relax\n",
    "from tvm.relax.transform import DeadCodeElimination\n",
    "from tvm.relax.transform import RemoveRedundantReshape\n",
    "from tvm.script import ir as I, relax as R\n",
    "\n",
    "def _run_pass_compare_output(Before, Expected):\n",
    "    fused_mod = RemoveRedundantReshape()(Before)\n",
    "    fused_mod = DeadCodeElimination()(fused_mod)\n",
    "    tvm.ir.assert_structural_equal(Expected, fused_mod)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "@I.ir_module\n",
    "class Before:\n",
    "    @R.function\n",
    "    def main(\n",
    "        x: R.Tensor((1, 1001, 1, 1), dtype=\"float16\")\n",
    "    ) -> R.Tensor((1, 1001), dtype=\"float16\"):\n",
    "        with R.dataflow():\n",
    "            lv: R.Tensor((1, 1001), dtype=\"float16\") = R.reshape(x, R.shape([1, 1001]))\n",
    "            lv1: R.Tensor((1, 1001), dtype=\"float16\") = R.reshape(lv, R.shape([1, 1001]))\n",
    "            gv: R.Tensor((1, 1001), dtype=\"float16\") = R.reshape(lv1, R.shape([1, 1001]))\n",
    "            R.output(gv)\n",
    "        return gv\n",
    "\n",
    "@I.ir_module\n",
    "class Expected:\n",
    "    @R.function\n",
    "    def main(\n",
    "        x: R.Tensor((1, 1001, 1, 1), dtype=\"float16\")\n",
    "    ) -> R.Tensor((1, 1001), dtype=\"float16\"):\n",
    "        with R.dataflow():\n",
    "            gv: R.Tensor((1, 1001), dtype=\"float16\") = R.reshape(x, R.shape([1, 1001]))\n",
    "            R.output(gv)\n",
    "        return gv\n",
    "\n",
    "_run_pass_compare_output(Before, Expected)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "@I.ir_module\n",
    "class Before:\n",
    "    @R.function\n",
    "    def main(\n",
    "        x: R.Tensor((1, 1001, 1, 1), dtype=\"float16\")\n",
    "    ) -> R.Tensor((1, 1001), dtype=\"float16\"):\n",
    "        with R.dataflow():\n",
    "            lv: R.Tensor((1, 1001, 1), dtype=\"float16\") = R.reshape(x, R.shape([1, 1001, 1]))\n",
    "            lv1: R.Tensor((1, 1001), dtype=\"float16\") = R.reshape(lv, R.shape([1, 1001]))\n",
    "            R.output(lv1)\n",
    "        return lv1\n",
    "\n",
    "@I.ir_module\n",
    "class Expected:\n",
    "    @R.function\n",
    "    def main(\n",
    "        x: R.Tensor((1, 1001, 1, 1), dtype=\"float16\")\n",
    "    ) -> R.Tensor((1, 1001), dtype=\"float16\"):\n",
    "        with R.dataflow():\n",
    "            lv1: R.Tensor((1, 1001), dtype=\"float16\") = R.reshape(x, R.shape([1, 1001]))\n",
    "            R.output(lv1)\n",
    "        return lv1\n",
    "\n",
    "_run_pass_compare_output(Before, Expected)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "@I.ir_module\n",
    "class Before:\n",
    "    @R.function\n",
    "    def main(\n",
    "        x: R.Tensor((1, 1001, 1, 1), dtype=\"float16\")\n",
    "    ) -> R.Tensor((1, 1001, 1, 1), dtype=\"float16\"):\n",
    "        with R.dataflow():\n",
    "            lv: R.Tensor((1, 1001, 1, 1), dtype=\"float16\") = R.reshape(\n",
    "                x, R.shape([1, 1001, 1, 1])\n",
    "            )\n",
    "            R.output(lv)\n",
    "        return lv\n",
    "\n",
    "@I.ir_module\n",
    "class Expected:\n",
    "    @R.function\n",
    "    def main(\n",
    "        x: R.Tensor((1, 1001, 1, 1), dtype=\"float16\")\n",
    "    ) -> R.Tensor((1, 1001, 1, 1), dtype=\"float16\"):\n",
    "        return x\n",
    "\n",
    "_run_pass_compare_output(Before, Expected)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xxx",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
